//
//  SEGDigitalComRootViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/5.
//

#import "SEGDigitalComRootViewModel.h"

@implementation SEGDigitalComRootViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}
- (void)racInit {
    
    @weakify(self);
    
    _requestDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            @strongify(self);
            
            //请求新用户房屋列表接口 包含用户认证的房屋，包含申请中、通过、未通过的房屋
            [self.requestManager requestAllMyHouseList:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                BOOL status = NO;
                if(isSuccessed && self.requestManager.houseInfoArray.count > 0){
                    status = YES;
                }
                [subscriber sendNext:@(status)];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _defaultHouseCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            @strongify(self);
            
            if (input) {
                
                SEGHouseInfoModel *model = self.dataArray[input.integerValue];
                
                [self.requestManager requestSetDefaultHouseWithHouseId:model.houseId relId:model.relId finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                    
                    if (isSuccessed) {
                        
                        [subscriber sendNext:nil];
                        
                        //更新用户信息
                        [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                        }];
                    }else {
                        [self.messageSubject sendNext:errorMessage];
                    }
                    [subscriber sendCompleted];
                }];
            }
            
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
}

#pragma mark - Publish Methods

#pragma mark - Private Methods

#pragma mark - Getter
- (SEGUserHouserRequestManager *)requestManager
{
    if (!_requestManager) {
        _requestManager = [[SEGUserHouserRequestManager alloc] init];
    }
    return _requestManager;
}

-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
